package com.ywgt.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.ywgt.pojo.Comment;
import com.ywgt.pojo.User;
import com.ywgt.pojo.UserInfo;
import com.ywgt.service.CommentService;
import com.ywgt.service.UserService;
import com.ywgt.util.ResponseUtil;
import io.swagger.annotations.ApiModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/wx/comment")
@ApiModel("小程序首页")
public class CommentController {

    @Autowired
    CommentService commentService;

    @Autowired
    UserService userService;


    /**
     * valueId=264&type=1&showType=0&page=1&size=5
     * @return
     */
    @GetMapping("/list")
    public Object getComments(Integer valueId, Integer type, Integer showType,
                              @RequestParam(required = false,defaultValue = "1") Integer page,
                              @RequestParam(required = false,defaultValue = "10") Integer size){
        Page<Comment> commentList= commentService.getCommentList(valueId,type,showType,page,size);
        List<Comment> items = commentList.getResult();
        long count = commentList.getTotal();
       List<Map<String,Object>> comments=new ArrayList<>();
        for (Comment item : items) {
            Map<String,Object> comment=new HashMap<>();
            comment.put("addTime",item.getAddTime());
            comment.put("content",item.getContent());
            comment.put("picList",item.getPicUrls());
            UserInfo userInfo = userService.getUserInfo(item.getUserId());
            comment.put("userInfo",userInfo);
            String reply=commentService.getReply(item.getId());
            comment.put("reply",reply);
            comments.add(comment);
        }
        Map<String,Object> data=new HashMap<>();
        data.put("count",count);
        data.put("currentPage",page);
        data.put("data",comments);
        return ResponseUtil.ok(data);
    }
}
